package edu.neusoft.android.yuejian14.model;

import java.util.List;

import edu.neusoft.android.yuejian14.Listener.RetrofitListener;
import edu.neusoft.android.yuejian14.javabean.Train;
import edu.neusoft.android.yuejian14.service.RetrofitService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static edu.neusoft.android.yuejian14.Const_Service.BASE_URL;

public class TrainModel {
    private Retrofit retrofit;
    public TrainModel(){
        //创建实例
        retrofit =new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    //培训列表
    public void gettrainlist(final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<List<Train>> call = retrofitService.gettrainlist();
        Callback<List<Train>> callback = new Callback<List<Train>>() {
            @Override
            public void onResponse(Call<List<Train>> call, Response<List<Train>> response) {
                List<Train> train = response.body();
                listener.onSuccess(train,1);
            }
            @Override
            public void onFailure(Call<List<Train>> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

//获取培训详情
    public void getTrainById(String trainid, final RetrofitListener listener){
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call call = retrofitService.getTrainById(trainid);
        Callback<Train> callback = new Callback<Train>() {
            @Override
            public void onResponse(Call<Train> call, Response<Train> response) {
                Train getTrainById = response.body();
                listener.onSuccess(getTrainById,1);
            }
            @Override
            public void onFailure(Call<Train> call, Throwable t) {
                listener.onFail();
            }
        };
        call.enqueue(callback);

    }

//搜索培训
public void getTrainBySearch(String trainName,final RetrofitListener listener){
    RetrofitService retrofitService = retrofit.create(RetrofitService.class);
    Call<List<Train>> call = retrofitService.getTrainBySearch(trainName);
    Callback<List<Train>> callback = new Callback<List<Train>>() {
        @Override
        public void onResponse(Call<List<Train>> call, Response<List<Train>> response) {
            List<Train> trainBean = response.body();
            listener.onSuccess(trainBean,1);
        }
        @Override
        public void onFailure(Call<List<Train>> call, Throwable t) {
            listener.onFail();
        }
    };
    call.enqueue(callback);
}

    public Call addTrain(int uid, int trainid){
        RetrofitService retrofitService= retrofit.create(RetrofitService.class);
        return retrofitService.addTrain(uid,trainid);
    }


}
